package com.qiu.aiteacherbacker.controller;

import com.qiu.aiteacherbacker.common.BaseResponse;
import com.qiu.aiteacherbacker.common.ErrorCode;
import com.qiu.aiteacherbacker.common.ResultUtils;
import com.qiu.aiteacherbacker.exception.ExceptionUtils;
import com.qiu.aiteacherbacker.model.domain.Question;
import com.qiu.aiteacherbacker.model.domain.request.AnswerGenerationRequest;
import com.qiu.aiteacherbacker.model.domain.request.ProblemGenerationRequest;
import com.qiu.aiteacherbacker.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.TimeZone;

@RestController
@RequestMapping("/AI")
@Api(tags = "AI管理模块")
//@CrossOrigin(origins = {"http://localhost:5173"}) // 允许跨域访问的域名
public class AiConstant {

    @Resource
    private QuestionService questionService;

    /**
     * 管理员使用AI生成问题
     *
     * @param problemGenerationRequest
     * @return
     */
    @PostMapping("/genQuestion")
    @ApiOperation(value = "生成问题")
    public BaseResponse<String> genQuestion(@RequestBody ProblemGenerationRequest problemGenerationRequest) {
        // 暂时不调用校验
//        if (!isLogin(request)) {
//            return ResultUtils.error(ErrorCode.NO_LOGIN);
//        }

        // teacherId 暂时不校验

        String ChatId = problemGenerationRequest.getChatId();
        String stage = problemGenerationRequest.getStage();
        String questions = problemGenerationRequest.getQuestions();
        int userId = problemGenerationRequest.getUserId();
        int countDirection = problemGenerationRequest.getCount_direction();
        String direction = problemGenerationRequest.getDirection();
        if (StringUtils.isAllBlank(stage, direction, questions)) {
            return ResultUtils.error(ErrorCode.PARAM_NOT_NULL);
        }
        if (countDirection < 0 || countDirection > 10) {
            countDirection = 5;
        }
        if (StringUtils.isAllBlank(ChatId)) {
            ChatId = String.valueOf(System.currentTimeMillis());
        }
        questionService.getQuestion(ChatId, stage, countDirection, direction, questions, userId);
        return ResultUtils.success("请求已经发送，请耐心等待5分钟");
    }

    /**
     * 根据用户ID获取问题
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/GetAllQuestion")
    @ApiOperation(value = "获取全部问题")
    public BaseResponse<List<Question>> getAllQuestion(@RequestParam(required = false) int userId) {
        return ResultUtils.success(questionService.getAllQuestion(userId));
    }


    @PostMapping("/genQuestionAnswer")
    @ApiOperation(value = "生成题解（不插入数据库）")
    public BaseResponse<String> genQuestionAnswer(@RequestBody AnswerGenerationRequest answerGenerationRequest) {
        int userId = answerGenerationRequest.getUserId();
        long questionId = answerGenerationRequest.getQuestionId();
        if (StringUtils.isAnyBlank(String.valueOf(userId), String.valueOf(questionId))) {
            ExceptionUtils.error(ErrorCode.PARAM_NOT_NULL, "用户ID || 问题ID 为空");
        }
        return ResultUtils.success(questionService.genQuestionAnswer(userId, questionId));
    }

}

